package com.pan.admin.module.test.controller;

import com.gitee.apanlh.annotation.viewresolver.RequestParamVo;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.gitee.apanlh.web.model.vo.ResultVO;
import com.gitee.apanlh.web.msg.ResultCode;
import com.gitee.apanlh.web.msg.ResultMsg;
import com.pan.admin.common.constant.test.ApiTestTypeEnum;
import com.pan.admin.module.test.entity.ApiTestSend;
import com.pan.admin.module.test.entity.vo.ApiTestUserFormVO;
import com.pan.admin.module.test.message.ApiTestResultMessage;
import com.pan.admin.module.test.service.ApiTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  API流程测试
 *  @author Pan
 */
@RestController
@RequestMapping("/test/api")
public class ApiTestClientController {

    @Autowired
    private ApiTestService apiTestService;

    /**
     *  发送测试流程
     *
     *  @param  requestVO   请求实体
     *  @return ResultVO
     */
    @PreAuthorize("@ss.hasPermi('http:api:test')")
    @PostMapping("/sendTest")
    public ResultVO<ApiTestResultMessage> sendTest(@RequestParamVo RequestVO requestVO) {
        String textType = requestVO.get("testType", String.class);

        ApiTestTypeEnum apiTestTypeEnum = ApiTestTypeEnum.getEnum(textType);
        if (apiTestTypeEnum == null) {
            return ResultVO.fail("非法类型");
        }

        //  发送测试流程
        ApiTestSend apiTestSend = requestVO.toBean(ApiTestSend.class);
        ApiTestResultMessage resultMessage = apiTestService.sendTest(apiTestSend, apiTestTypeEnum);
        return ResultVO.suc(resultMessage);
    }

    /**
     *  发起半流程回调进件模式
     *
     *  @return ResultVO
     */
    @PreAuthorize("@ss.hasPermi('http:api:test')")
    @PostMapping("/sendPullApplyNotice")
    public ResultVO<ApiTestResultMessage> sendPullApplyNotice(@RequestParamVo RequestVO requestVO) {
        //  发起半流程回调进件模式
        ApiTestSend apiTestSend = requestVO.toBean(ApiTestSend.class);
        ApiTestResultMessage resultMessage = apiTestService.sendTest(apiTestSend, ApiTestTypeEnum.API_PLAT_PULL_APPLY_V2);
        return ResultVO.suc(resultMessage);
    }

    /**
     *  获取测试用户表单数据
     *
     *  @return ResultVO
     */
    @PreAuthorize("@ss.hasPermi('http:api:test')")
    @GetMapping("/listUserForm")
    public ResultVO<List<ApiTestUserFormVO>> listUserForm() {
        List<ApiTestUserFormVO> list = apiTestService.listUserForm();
        return ResultVO.suc(list);
    }

    /**
     *  生成测试手机号
     *
     *  @return ResultVO
     */
    @PreAuthorize("@ss.hasPermi('http:api:test')")
    @GetMapping("/getGenTestPhone")
    public ResultVO<String> getGenTestPhone() {
        String phone = apiTestService.genTestPhone();
        return ResultVO.suc(ResultCode.SUC_CODE, ResultMsg.SUC_MSG, phone);
    }
}
